package com.webinsky.web;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RequestPredicates.DELETE;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

import com.webinsky.web.resource.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.webinsky.web.filter.RequestFilter;

@Configuration
public class RouteConfig {
	/**
	 * 用户资源API route配置
	 * @param userResource
	 * @param reqFilter
	 * @return
	 */
	@Bean
	public RouterFunction<ServerResponse> userRouteWithFilter(UserResource userResource, RequestFilter reqFilter) {
		return 	route(GET("/api/user/logout"), userResource::logout).
					andRoute(POST("/api/user/login"), userResource::login).
						andRoute(GET("/api/user/checklogin"), userResource::checkLogin).
							andRoute(POST("/api/user/adminverfy"), userResource::adminLoginVerify).
								andRoute(GET("/api/keepsession"), userResource::keepsession);
	}

	@Bean
	public RouterFunction<ServerResponse> tagWithFilter(CatalogResource catalogResource, RequestFilter reqFilter) {
		return 	route(POST("/api/tag/category/create"), catalogResource::createCatalog).
					andRoute(POST("/api/tag/category/update"), catalogResource::updateCatalog).
						andRoute(POST("/api/tag/category/delete"), catalogResource::deleteCatalog).
							andRoute(POST("/api/tag/category/search"), catalogResource::searchCatalogs).
								andRoute(POST("/api/tag/create"), catalogResource::createTag).
									andRoute(POST("/api/tag/update"), catalogResource::updateTag).
										andRoute(POST("/api/tag/delete"), catalogResource::deleteTag).
											andRoute(POST("/api/tag/search"), catalogResource::searchTag).

												andRoute(POST("/api/sync"), catalogResource::syncTags).
													andRoute(POST("/api/tag/categories"), catalogResource::batchCreateCatalogs).
													andRoute(POST("/api/tag/category"), catalogResource::searchCatalog).
													andRoute(POST("/api/tag/category/del"), catalogResource::delCatalog).
														andRoute(POST("/api/tag/tags"), catalogResource::batchCreateTags).
														andRoute(POST("/api/tag/tag"), catalogResource::searchTagLang).
														andRoute(POST("/api/tag/tag/del"), catalogResource::delTag)
											
				;
	}

	@Bean
	public RouterFunction<ServerResponse> articleWithFilter(ArticleResource articleResource, RequestFilter reqFilter) {
		return 	route(POST("/api/article/create"), articleResource::createOrUpdate).
					andRoute(POST("/api/article/exist"), articleResource::isExistArticle).
						andRoute(POST("/api/article"), articleResource::getArticle).
							andRoute(POST("/api/article/list"), articleResource::getArticleList).
								andRoute(POST("/api/article/delete"), articleResource::deleteArticle).
									andRoute(POST("/api/article/export"), articleResource::exportExcel).
									andRoute(POST("/api/article/can/modify"), articleResource::setArticleCanModify);
	}

	@Bean
	public RouterFunction<ServerResponse> folderWithFilter(FolderResource folderResource, RequestFilter reqFilter) {
		return 	route(POST("/api/folder/create"), folderResource::addFolder).
				andRoute(POST("/api/folder/update"), folderResource::changeFolderName).
				andRoute(POST("/api/folder/delete"), folderResource::deleteFolder).
				andRoute(POST("/api/folder/list"), folderResource::getBookFolders).
				andRoute(POST("/api/folder/sort"), folderResource::redirectFolderPosition).
				andRoute(POST("/api/folder/article/add"), folderResource::addArticleToFolder).
				andRoute(POST("/api/folder/article/remove"), folderResource::deleteArticleFromFolder).
				andRoute(POST("/api/folder/articles"), folderResource::getArticles).
				andRoute(POST("/api/folder/item/articles"), folderResource::getItemArticles).
				andRoute(POST("/api/folder/article/sort"), folderResource::redirectArticlePosition).
				andRoute(POST("/api/folder/console/index"), folderResource::getAllArticlesCount).
				andRoute(POST("/api/folder/batch"), folderResource::addFolders).
				andRoute(POST("/api/folder"), folderResource::getFolders).
				andRoute(POST("/api/folder/del"), folderResource::delFolder);
	}

	@Bean
	public RouterFunction<ServerResponse> userWithFilter(UserResource userResource, RequestFilter reqFilter) {
		return 	route(POST("/api/user/create"), userResource::createAccount).
				andRoute(POST("/api/user/update"), userResource::updateAccount).
				andRoute(POST("/api/user/delete"), userResource::deleteAccount).
				andRoute(POST("/api/user/list"), userResource::filterAccount);
	}
	
	@Bean
	public RouterFunction<ServerResponse> info(InfoResource infoResource) {
		return 	route(POST("/api/infos"), infoResource::submitInfo).
				andRoute(DELETE("/api/infos/{id}"), infoResource::deleteInfo).
				andRoute(POST("/api/infos/filter"), infoResource::queryInfosByFilter);
	}
	
	@Bean
	public RouterFunction<ServerResponse> site(SiteResource siteResource) {
		return 	route(GET("/api/sites"), siteResource::getSites).
				andRoute(POST("/api/sites"), siteResource::createSite).
				andRoute(POST("/api/sites/active"), siteResource::enableDisableSite);
	}
	
	@Bean
	public RouterFunction<ServerResponse> email(EmailSettingResource emailSettingResource) {
		return route(GET("/api/eamils/email"), emailSettingResource::getSettings).
				andRoute(POST("/api/emails"), emailSettingResource::createOrUpdateSetting).
				andRoute(GET("/api/emails/test"), emailSettingResource::testSendEmail);
	}

//	@Bean
//	public RouterFunction<ServerResponse> staticWithFilter(StaticResource staticResource, StatisticsResource statis, RequestFilter reqFilter) {
//		return 	route(GET("/api/statis/workstation"), statis::statisticsWorkStation);
//	}

//	@Bean
//	public RouterFunction<ServerResponse> staticWithFilter(StaticResource staticResource, StatisticsResource statis, RequestFilter reqFilter) {
//		return 	route(POST("/api/static/publish"), staticResource::publish).
//				andRoute(POST("/api/static/current"), staticResource::currentVersion).
//				andRoute(POST("/api/static/history"), staticResource::historyVersion).
//				andRoute(GET("/api/statis/workstation"), statis::statisticsWorkStation);
//	}
	
}
